<template>
  <div class="box">
    <Loading v-if="chinicList.chinic_id === undefined" />
    <div v-else>
      <div class="videoDetail_title">
        <!-- <span>{{chinicList.chinic_introduce}}</span> -->
        <span class="iconfont icon-close" @click="goBack"></span>
      </div>
      <div class="videoDetail_main" @click="videoPlay">
        <video id="video" :src="chinicList.chinic_video"></video>
      </div>
      <div class="videoDetail_bottom">
        <div class="progress">
          <!-- <van-progress :percentage="percentage" :show-pivot="false" stroke-width="2px" /> -->
          <van-slider class="slider" v-model="percentage" @change="onChange">
            <template #button>
              <div class="custom-button"></div>
            </template>
          </van-slider>
          <div class="countDownTimer">{{countDownTimer}}</div>
        </div>
        <div class="videoDetail_footer">
          <img v-if="proSelectBool" src="@/assets/images/proPick.png" alt="" @click="changeProSelect">
          <img v-else src="@/assets/images/proPickNo.png" alt="" @click="changeProSelect">
          <img v-if="videoPlayBool" src="@/assets/images/videoPlay.png" alt="" @click="changeVideoPlay">
          <img v-else src="@/assets/images/videoStop.png" alt="" @click="changeVideoPlay">
          <span>{{chinicList.chinic_num + '人已看'}}</span>
        </div>
      </div>
      <div class="videoDetail_pro" v-show="proSelectBool" @click="goProDetail(proList.proid)">
        <img :src="proList.pro_pic" alt="">
        <div class="videoDetail_pro_detail">
          <p>{{proList.pro_introduce}}</p>
          <p>
            <span>{{proList.clinic_name}}</span>
            <span>{{proList.distance + 'km'}}</span>
          </p>
          <!-- <p>{{proList.clinic_address}}</p> -->
          <p>
            <span>{{'￥' + proList.pro_price}}</span>
            <span>{{proList.subscribe + '预约'}}</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Loading from '../../components/loading'
import { getProductById, getVideoKindProDetailList } from '../../api'
import Vue from 'vue'
import { Icon, Slider } from 'vant'
Vue.use(Icon)
Vue.use(Slider)
export default {
  data () {
    return {
      chinic_id: '',
      chinicList: [],
      proList: [],
      proSelectBool: true,
      videoPlayBool: true,
      videoTimer: 0,
      percentage: 0,
      timer: '',
      countDownTimer: ''
    }
  },
  components: {
    Loading
  },
  computed: {},
  mounted () {
    this.chinic_id = this.$route.query.chinicId
    getVideoKindProDetailList({ chinic_id: this.chinic_id }).then(res => {
      if (res.data.code === '200') {
        this.chinicList = res.data.data[0]
        getProductById({
          proid: this.chinicList.chinic_recommendID,
          username: localStorage.getItem('userTel_AM')
        }).then(res => {
          if (res.data.code === '200') {
            this.proList = res.data.data[0]
          }
        })
      }
    })
    this.getSeconds(this.videoTimer)
  },
  methods: {
    changeProSelect () {
      this.proSelectBool = !this.proSelectBool
    },
    changeVideoPlay () {
      this.videoPlayBool = !this.videoPlayBool
      // 控制单个视频的播放暂停(独立)
      var vide = document.getElementById('video')
      if (vide.paused) {
        vide.play()
        this.openTimer(vide)
      } else if (vide.play()) {
        vide.pause()
        this.stopTimer(vide)
        // this.getSeconds(this.videoTimer - vide.currentTime)
      }
      this.videoTimer = vide.duration
      this.getSeconds(this.videoTimer)
    },
    videoPlay () {
      this.videoPlayBool = !this.videoPlayBool
      var vide = document.getElementById('video')
      if (vide.paused) {
        vide.play()
        this.openTimer(vide)
      } else if (vide.play()) {
        vide.pause()
        this.stopTimer(vide)
        // this.getSeconds(this.videoTimer - vide.currentTime)
      }
      this.videoTimer = vide.duration
      this.getSeconds(this.videoTimer)
    },
    goProDetail (proid) {
      this.$router.push({ path: '/proDetail', query: { proid } })
    },
    onChange (e) {
      this.percentage = e
      var vide = document.getElementById('video')
      vide.currentTime = parseInt(this.percentage / 100 * this.videoTimer)
    },
    goBack () {
      this.$router.go(-1)
    },
    openTimer (vide) {
      this.timer = setInterval(() => {
        this.percentage = parseInt(vide.currentTime) / parseInt(this.videoTimer) * 100
        this.getSeconds(this.videoTimer - vide.currentTime)
        if (this.percentage >= 100) {
          this.stopTimer(vide)
        }
      }, 1000)
    },
    stopTimer (vide) {
      clearInterval(this.timer)
    },
    getSeconds (s) {
      var sTime = parseInt(s)
      var mTime = 0
      var hTime = 0
      if (sTime > 60) {
        mTime = parseInt(sTime / 60)
        sTime = parseInt(sTime % 60)
        if (mTime > 60) {
          hTime = parseInt(mTime / 60)
          mTime = parseInt(mTime % 60)
        }
      }
      var result = ''
      if (sTime >= 0 && sTime < 10) {
        result = '0' + parseInt(sTime) + ''
      } else {
        result = '' + parseInt(sTime) + ''
      }
      if (mTime >= 0 && mTime < 10) {
        result = '0' + parseInt(mTime) + ':' + result
      } else {
        result = '' + parseInt(mTime) + ':' + result
      }
      if (hTime >= 0 && hTime < 10) {
        result = '0' + parseInt(hTime) + ':' + result
      } else {
        result = '' + parseInt(hTime) + ':' + result
      }
      this.countDownTimer = result
    }
  }
}
</script>

<style lang="scss" scoped>
.videoDetail_title {
  width: 100%;
  height: 0.86rem;
  /* span:nth-of-type(1) {
    float: left;
    max-width: 3rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0.27rem 0;
    padding-left: 0.15rem;
    line-height: 0.32rem;
    font-size: 0.14rem;
  } */
  span {
    font-size: 0.32rem;
    line-height: 0.32rem;
    width: 0.32rem;
    height: 0.32rem;
    float: right;
    margin: 0.27rem 0.15rem;
  }
}
.videoDetail_main {
  width: 100%;
  height: auto;
  video {
    width: 100%;
  }
}
.videoDetail_bottom {
  width: 100%;
  height: 0.84rem;
  position: fixed;
  bottom: 0;
  border-top: 1px solid #f5f5f5;
  .progress {
    width: 100%;
    height: 0.18rem;
    padding: 0 0.15rem;
    .slider {
      width: 83%;
      float: left;
      margin-right: 2%;
      margin-top: 0.06rem;
    }
    .countDownTimer {
      text-align: right;
      width: 15%;
      float: left;
      line-height: 0.18rem;
    }
  }
  .videoDetail_footer {
    width: 100%;
    height: 0.66rem;
    padding: 0.17rem 0.3rem;
    img:nth-of-type(1) {
      float: left;
      width: 0.27rem;
      height: 0.27rem;
      margin: 0.02rem 0;
    }
    img:nth-of-type(2) {
      float: left;
      width: 0.32rem;
      height: 0.32rem;
      margin-left: 1.15rem;
    }
    span {
      float: right;
      color: #999999;
      font-size: 0.12rem;
      line-height: 0.32rem;
    }
  }
}
.videoDetail_pro {
  width: 100%;
  height: 1.22rem;
  padding: 0.2rem 0.25rem;
  background: #fff8f8;
  border-radius: 0.04rem;
  position: fixed;
  bottom: 1rem;
  z-index: 20;
  box-shadow:0px 4px 5px 4px rgba(150, 150, 150, 0.2);
  img {
    float: left;
    margin-right: 0.1rem;
    width: 0.82rem;
    height: 0.82rem;
    border-radius: 0.05rem;
  }
  .videoDetail_pro_detail {
    width: 2.33rem;
    float: left;
    p:nth-of-type(1) {
      width: 100%;
      height: 0.32rem;
      line-height: 0.16rem;
      font-size: 0.14rem;
      color: #646363;
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      margin-bottom: 0.04rem;
    }
    p:nth-of-type(2) {
      width: 100%;
      height: 0.14rem;
      line-height: 0.14rem;
      font-size: 0.12rem;
      color: #787777;
      margin: 11px 0 11px 0;
      span:nth-of-type(1) {
        float: left;
        max-width: 80%;
        margin-right: 0.1rem;
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
      }
      span:nth-of-type(2) {
        line-height: 0.14rem;
        float: right;
      }
    }
    /* p:nth-of-type(3) {
      width: 100%;
      height: 0.14rem;
      line-height: 0.14rem;
      font-size: 0.12rem;
      color: #9a9898;
      overflow: hidden;
      text-overflow:ellipsis;
      white-space: nowrap;
      margin-bottom: 0.04rem;
    } */
    p:nth-of-type(3) {
      width: 100%;
      height: 0.14rem;
      line-height: 0.14rem;
      span:nth-of-type(1) {
        float: left;
        color: #ff7070;
        font-size: 0.14rem;
      }
      span:nth-of-type(2) {
        float: right;
        color: #939191;
        font-size: 0.12rem;
      }
    }
  }
}
.custom-button {
  width: 0.1rem;
  height: 0.1rem;
  background: #ff5959;
  border-radius: 50%;
}
</style>
